package com.hmy.common.timer;

import com.hmy.common.timer.retry.SendMsgWithRetryTask;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class TimerMain {
    public static void main(String[] args) {

        // 时间轮测试
        // 1、创建时间轮
        // 默认tickDuration(走过一个格子所需要的时间): 100毫秒 -> 100000000纳秒
        //    tickPerWheel(时间轮上共有多少个格子): 512
        HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();

        // 2、创建任务
        ShowMemoryTask showMemoryTask1 = new ShowMemoryTask();
        ShowMemoryTask showMemoryTask2 = new ShowMemoryTask();

        // 3、添加任务至时间轮 添加后会自行启动
        hashedWheelTimer.newTimeout(showMemoryTask1, 3, TimeUnit.SECONDS);
        hashedWheelTimer.newTimeout(showMemoryTask2, 4, TimeUnit.SECONDS);



    }

    @Test
    public void retryTest() {
        HashedWheelTimer timer = new HashedWheelTimer();
        SendMsgWithRetryTask sendMsgTask = new SendMsgWithRetryTask();
        sendMsgTask.setRetryTimes(2);
        SendMsgWithRetryTask sendMsgTask1 = new SendMsgWithRetryTask();
        sendMsgTask1.setRetryTimes(1);
        timer.newTimeout(sendMsgTask, 1, TimeUnit.SECONDS);
        timer.newTimeout(sendMsgTask1, 2, TimeUnit.SECONDS);
        try {
            TimeUnit.SECONDS.sleep(15);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}


